// pages/sita/setting/setting.js
const app = getApp()


Page({

  /**
   * 页面的初始数据
   */
  data: {
    position:0,
    paybill:100,
    count:0,
    latex:''
    },
  wxLogin:function(){
    var that = this;    
    wx.login({
      success: res => {
        console.log(res)
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        this.data.jsCode = res.code 
        console.log(res.code)   
        wx.request({
          url: 'https://genious.sitacloud.com/wx_account_logon',
          header:{
            'content-type': 'application/json',
          },
          method:'POST',
          data:{
            "wxLogonJSCode":res.code
          },success(res){
            wx.hideLoading()
            console.log(res.data)
            if(res.data.statuscode == 200){
              var loginData = {
                OpenID:res.data.openid
              }
              app.loginSuccess(loginData)
              wx.switchTab({
                url: '/pages/sita/chat/chat',
              })
            }else{
              wx.showToast({
                title: '登录失败',
                icon:'error'
              })
            }       
          },fail(res){
            console.log(res)   
            wx.hideLoading()
            wx.showToast({
              title: '登录失败',
              icon:'error'
            })
          }
        })
      },
      fail(res){
        wx.hideLoading()
        that.loginError();
        
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   
   
  },

   /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
   
    if (app.globalData.OpenID == "" || app.globalData.OpenID == undefined) {
      wx.showModal({
        title: '',
        content: '请您登录验证',
        confirmText: '登录',
        complete: (res) => {
          if (res.cancel) {
            wx.redirectTo({
              url: '/pages/login/atLogin/atLogin',
            })
          }
          if (res.confirm) {
            wx.redirectTo({
              url: '/pages/login/atLogin/atLogin',
            })
          }
        }
      })
      return
    }else{
      this.getUseCount()
    }
  
  },

  inviting_req(){
    wx.navigateTo({
      url: '/pages/sita/shareImg/shareImg',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  itemClick(res){
    this.setData({
      position:res.currentTarget.dataset.index,
      paybill:res.currentTarget.dataset.item.payment
    })
  },
  
  paybill(){
    wx.navigateTo({
      url: '/pages/sita/pay/paybill',
    })
  },
   removeQuotes(str) {
    return str.replace(/^"|"$/g, '');
  },
  findCount(){
    console.log("call findcount")
    this.getUseCount()
  },
  getUseCount(){
    var that = this
    wx.request({
      url: app.app_url.BASE_URL+app.app_url.SITA_USE_COUNT,
      method:'POST',
      data:{
        'openid':app.globalData.OpenID
      },
      success(res){
      console.log(res)
       if(res.data.status==400||res.data.status==200){
         that.setData({
          count:res.data.total
         })
       }
      },fail(res){
        console.log(res)
      }

    })
  }
})


  
